package com.api.user.controller;
import com.api.base.model.SysUserRole;
import com.api.base.service.SysUserRoleService;
import com.api.core.controller.Ctrl;
import com.api.core.response.Result;
import com.api.core.response.ResultGenerator;
import com.api.user.model.Message;
import com.api.user.model.MessageAdd;
import com.api.user.service.MessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.api.core.annotation.PowerEnable;
import io.swagger.annotations.*;



/**
* Created by wanghuiwen on 2021/03/29.
*/
@PowerEnable(name = "消息",url = "/message")
@Api(value = "消息", tags = {"消息"})
@RestController
@RequestMapping("/message")
public class MessageController extends Ctrl{
    @Resource
    private MessageService messageService;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @ApiOperation(value = "消息添加", tags = {"消息"}, notes = "消息添加")
    @PostMapping(value="/add",name="消息添加")
    public Result add(@ApiParam MessageAdd messageAdd) {
        messageService.add(messageAdd);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "删除/收藏/阅读消息", tags = {"消息"}, notes = "删除/收藏/阅读消息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "messageId",required=true, value = "消息id", dataType = "Long", paramType = "query"),
        @ApiImplicitParam(name = "state",required=true, value = "状态", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/user/update",name="删除/收藏/阅读消息")
    public Result userUpdate(@RequestParam String messageId,@RequestParam Integer state, Authentication authentication) {
        Long userId = getAuthUser(authentication).getId();
        String[] ids = messageId.split(",");
        for (String id : ids) {
            messageService.userUpdate(userId,Long.valueOf(id),state);
        }
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "管理员删除消息", tags = {"消息"}, notes = "管理员删除消息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "messageId",required=true, value = "消息id", dataType = "Long", paramType = "query")
    })
    @PostMapping(value="/delete",name="管理员删除消息")
    public Result delete(@RequestParam Long messageId) {
        messageService.delete(messageId);
        return ResultGenerator.genSuccessResult();
    }



    @ApiOperation(value = "消息列表信息", tags = {"消息"}, notes = "消息列表信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "messageType", value = "消息类型", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "messageTitle", value = "标题", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "createTimeStart", value = "开始日期", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "createTimeEnd", value = "结束日期", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/log", name = "消息列表信息")
    public Result log(
            @RequestParam(required = false) String messageType,
            @RequestParam(required = false) String messageTitle,
            @RequestParam(required = false) String createTimeStart,
            @RequestParam(required = false) String createTimeEnd,
                       @RequestParam(defaultValue = "{}") String order,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> params = new HashMap<>();
        params.put("messageType",messageType);
        params.put("messageTitle",messageTitle);
        params.put("createTimeStart",createTimeStart);
        params.put("createTimeEnd",createTimeEnd);
        return messageService.list(params, order, page, size);
    }


    @ApiOperation(value = "用户消息", tags = {"消息"}, notes = "用户消息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "state", value = "状态", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "messageType", value = "类型", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "favorites", value = "收藏", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "messageTitle", value = "标题", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "messageSort", value = "排序", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "order", value = "排序json", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "page", value = "页码", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示的条数", dataType = "String", paramType = "query", defaultValue = "10")
    })
    @PostMapping(value = "/user/list", name = "用户消息")
    public Result userList(
                    @RequestParam(required = false) Integer state,
                    @RequestParam(required = false) String messageType,
                    @RequestParam(required = false) String favorites,
                    @RequestParam(required = false) String messageTitle,
                    @RequestParam(required = false) String messageSort,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size, Authentication authentication) {



        Map<String, Object> params = new HashMap<>();
        params.put("state",state);
        params.put("source",messageType);
        params.put("favorites",favorites);
        params.put("messageTitle",messageTitle);
        params.put("userId",getAuthUser(authentication).getId());
        Map<String, Object> orderParams = new HashMap<>();
        if(StringUtils.isNotEmpty(messageSort)){
            orderParams.put("create_time",messageSort);
        }

        return messageService.userList(getAuthUser(authentication),params, orderParams, page, size);
    }
}
